package com.tedu.servies;

import com.tedu.dao.TopicMapper;
import com.tedu.pojo.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service  //使用注解，把服务层实现类交给  spring管理
public class TopicServiesImpl implements TopicServies {

    // mapper  接口
    @Autowired
    TopicMapper topicMapper;


    @Override
    public List<Topic> getAllTopic() {
        return topicMapper.getAllTopic();
    }

    /**
     * 通过id 获取一个新闻类型实体类
     * @param topicID
     * @return
     */
    @Override
    public Topic getTopicById(int topicID) {

        List<Topic> data = topicMapper.getTopicById(topicID);
        if(data.size()>0)
        {
            return data.get(0);
        }
        return null;
    }

    /**
     * 随机取出10个
     * @return
     */
    @Override
    public List<Topic> getTopic() {
        //调用dao  获取全部的信息类
        List<Topic> topic = topicMapper.getTopic();
        //随机取10
        List<Topic> data = new ArrayList<>();
        if(topic.size()<=10){
            return topic;
        }
        for (int i = 0; i < 10; i++) {
            //随机取出一个信息类型
            Topic topic1 = topic.get(new Random().nextInt(topic.size()));
            topic.remove(topic1);
            data.add(topic1);
        }
        return data;
    }
}
